<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/7/2
 * Time: 13:46
 */

namespace Admin\Controller;


use Admin\Api\AngelRoleApi;
use Admin\Api\DatatreeApi;
use Admin\Api\MemberApi;
use Admin\Api\NewmemberApi;
use Admin\Api\PartnerRoleApi;
use Admin\Api\RoleApi;
use Shop\Api\WalletApplyApi;
use Shop\Model\WalletApplyModel;
use Shop\Api\WalletHisApi;
use Admin\Api\MessageApi;
use Admin\Model\MessageModel;
use Ucenter\Api\UserLogApi;
use Weixin\Api\WxuserApi;

class VipManageController extends AdminController
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /*
     * vip列表
     * */
    public function viplist(){
        $vip = I('vip','');
        $name = I('name','');
        $this->assign('vip',$vip);
        $this->assign('name',$name);
        if($vip == -2){
            $map = array(
                'itboye_new_member.role_grade' => '-2',
                'itboye_new_member.vip_type' => '1',
            );
        }

        if(($vip == -1)){
            $map = array(
                'itboye_new_member.role_grade' => '-1',
                'itboye_new_member.vip_type' => '1',
            );
        }

        if(($vip == 1)){
            $map = array(
                'itboye_new_member.role_grade' => '1',
                'itboye_new_member.vip_type' => '1',
            );
        }

        if(($vip == 2)){
            $map = array(
                'itboye_new_member.role_grade' => '2',
                'itboye_new_member.vip_type' => '1',
            );
        }

        if(($vip == 3)){
            $map = array(
                'itboye_new_member.role_grade' => '3',
                'itboye_new_member.vip_type' => '1',
            );
        }

        if(($vip == 4)){
            $map = array(
                'itboye_new_member.role_grade' => '4',
                'itboye_new_member.vip_type' => '1',
            );
        }

        if(($vip == 5)){
            $map = array(
                'itboye_new_member.role_grade' => '1',
                'itboye_new_member.vip_type' => '2',
            );
        }

        if(($vip == 6)){
            $map = array(
                'itboye_new_member.role_grade' => '1',
                'itboye_new_member.vip_type' => '3',
            );
        }
        $map['itboye_new_member.name'] = array('like','%'.$name.'%');
        $res = apiCall(NewmemberApi::UNION_WXUSER_QUERY,array($map,array('curpage'=>I('p','0'),'size'=>10)));
        if($res['status']){
            $this->assign('show',$res['info']['show']);
            $this->assign('list',$res['info']['list']);
        }

        $this->display();
    }

    /*
     * 升为天使会员
     * */
    public function riseAngel(){
        $wx_id = I('wx_id');
        $map['wx_id'] = $wx_id;
        $money = I('money');
        //查询天使vip角色权益
        $angelres = apiCall(AngelRoleApi::GET_INFO,array(array('id' => '1')));
        $newmemberres = apiCall(NewmemberApi::GET_INFO,array($map));

        if($angelres['status']){
            if($newmemberres['status']){
                $store_money = $money + $newmemberres['info']['store_money'];
                $entity = array(
                    'role_grade' => '1',
                    'store_money' => $store_money,
                    'invite_power' => '1',
                    's_gold_mumber' => $angelres['info']['become_gold'],
                    's_platina_mumber' => $angelres['info']['become_platina'],
                    'vip_type' => '2',
                    'invitation_uid' => '0',
                );
                $res = apiCall(NewmemberApi::SAVE,array($map,$entity));
                if($res['status']){
                    $add = array(
                        'op_type' => '99',
                        'number' => $money,
                        'time' => time(),
                        'uid' =>$map['wx_id'],
                        'note' => '成为天使会员的储值',
                        'num_type' => '1'
                    );
                    $logres = apiCall(UserLogApi::ADD,array($add));

                    //增加消费股
                    $this->ajaxReturnSuc('授权成功');
                }else{
                    $this->ajaxReturnErr('授权失败');
                }
            }

        }

    }


    /*
    * 升为股东会员
    * */
    public function risePartner(){
        $wx_id = I('wx_id');
        $map['wx_id'] = $wx_id;
        $money = I('money');
        //查询天使vip角色权益
        $angelres = apiCall(PartnerRoleApi::GET_INFO,array(array('id' => '1')));
        $newmemberres = apiCall(NewmemberApi::GET_INFO,array($map));
        if($newmemberres['status']){
            if($angelres['status']){
                $entity = array(
                    'role_grade' => '1',
                    'invite_power' => '1',
                    'store_money' => $newmemberres['info']['store_money'] + $money,
                    's_gold_mumber' => $angelres['info']['become_gold'],
                    's_platina_mumber' => $angelres['info']['become_platina'],
                    'vip_type' => '3',
                    'invitation_uid' => '0',
                );
                $res = apiCall(NewmemberApi::SAVE,array($map,$entity));

                if($res['status']){
                    $add = array(
                        'op_type' => '99',
                        'number' => $money,
                        'time' => time(),
                        'uid' =>$map['wx_id'],
                        'note' => '成为股东会员的储值',
                        'num_type' => '1'
                    );
                    $logres = apiCall(UserLogApi::ADD,array($add));
                    $this->ajaxReturnSuc('授权成功');
                }else{
                    $this->ajaxReturnErr('授权失败');
                }
            }
        }

    }

    /*
     * 编辑会员资料以及储值
     * */
    public function edit_account(){

        $map['wx_id'] = I('wx_id');
        if(IS_GET){
            $info = apiCall(NewmemberApi::GET_INFO,array($map));
            if($info['status']){
                $this->assign($info['info']);
            }
            $this->display();
        }else{
            $save = array(
                'store_money' => I('store_money'),
                'sham_share' => I('sham_share'),
                'real_shares' => I('real_shares'),
            );
            $info = apiCall(NewmemberApi::SAVE,array($map,$save));
            if($info['status']){
               $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    /*
     * 增加储值
     * */
    public function add_store_money(){
        if(IS_POST){
            $add_money = I('money');
            if(empty($add_money)){
                $this->error('储值不能为空');
            }
            $map['wx_id'] = I('wx_id');
            $info = apiCall(NewmemberApi::GET_INFO,array($map));
            if($info['status']){
                $modal = M();
                $modal->startTrans();
                $flag = false;
                $lastmongey = $info['info']['store_money'];
                $add['store_money'] = $lastmongey+$add_money;
                $addres = apiCall(NewmemberApi::SAVE,array($map,$add));
                if(!$addres['status']){
                    $flag = true;
                }


                //根据储值进行VIP升级

                $vip =  $info['info']['vip_type'];
                $gradeid = $info['info']['role_grade'];
                if($vip == 1){
                    $map['save_money'] = array(
                        'ELT' ,$add_money
                    );

                    $res = apiCall(RoleApi::GET_INFO,array($map,'save_money desc'));

                    if($res['status']){
                        if($gradeid < $res['info']['id'] && $res['info']['id'] > 1){
                            //进行升级
                            if($res['info']['id'] == 2){//消费股的分配和积分
                                $save['points'] = $res['info']['obtain_points'];
                            }

                            if($res['info']['id'] == 3){//消费股的分配和积分
                                $save['points'] = $res['info']['obtain_points']+$info['info']['points'];
                                $save['sham_share'] = $res['info']['cumulative_number']+$info['info']['sham_share'];
                            }

                            if($res['info']['id'] == 4){//消费股的分配和积分
                                $save['points'] =$res['info']['obtain_points']+$info['info']['points'];
                                $save['sham_share'] = $res['info']['cumulative_number']+$info['info']['sham_share'];
                            }

                            $save['buy_num'] = '-1';
                            $save['role_grade'] = $res['info']['id'];
                            $save['invite_power'] = '1';
                            $save['s_gold_mumber'] = $res['info']['become_gold'];

                            $save['s_platina_mumber'] = $res['info']['become_platina'];

                            $newmap['wx_id'] = $info['info']['wx_id'];
                            $res = apiCall(NewmemberApi::SAVE,array($newmap,$save));

                        }
                    }
                }

                //判断是否是被邀请的的增加VIP奖励
                if($info['info']['invitation_uid'] != 0){
                    $storemoneymap = array(
                        'uid'=>$map['wx_id'],
                        'op_type'=>'99',
                    );
                    $resstore = apiCall(UserLogApi::COUNT,array($storemoneymap));

                    if($resstore['status']){
                        if($resstore['info'] == 0){//判断是否是第一次进行储值

                            $invitmap = array(
                                'wx_id' => $info['info']['invitation_uid'],
                            );

                            $invitinfores = apiCall(NewmemberApi::GET_INFO,array($invitmap));

                            if($invitinfores['status']){
                                $vip_type = $invitinfores['info']['vip_type'];
                                $rolemap['id'] = $invitinfores['info']['role_grade'];
                                if($vip_type == 1){
                                    $roleres = apiCall(RoleApi::GET_INFO,array($rolemap));

                                    if($roleres['status']){
                                        $vip_reward = $roleres['info']['vip_reward'];
                                        $this->vip_reward($map['wx_id'],$vip_reward,$add_money);
                                    }
                                }

                                if($vip_type == 2){
                                    $roleres = apiCall(AngelRoleApi::GET_INFO,array($rolemap));
                                    if($roleres['status']){
                                        $vip_reward = $roleres['info']['vip_reward'];
                                        $this->vip_reward($map['wx_id'],$vip_reward,$add_money);
                                    }
                                }

                                if($vip_type == 3){
                                    $roleres = apiCall(PartnerRoleApi::GET_INFO,array($rolemap));
                                    if($roleres['status']){
                                        $vip_reward = $roleres['info']['vip_reward'];
                                        $this->vip_reward($map['wx_id'],$vip_reward,$add_money);
                                    }
                                }
                            }

                        }
                    }

                }

                //增加储值记录
                $logadd = array(
                    'op_type'=>'99',
                    'number'=>$add_money,
                    'time'=>time(),
                    'uid'=>$map['wx_id'],
                    'note'=>'后台增加的储值',
                    'num_type'=>'1'
                );

                $logres = apiCall(UserLogApi::ADD,array($logadd));

                if(!$logres['status']){
                    $flag = true;
                }

                if($flag){
                    $modal->rollback();
                    $this->error('储值失败');
                }else{
                    $modal->commit();
                    $this->success('储值成功',U('VipManage/viplist'));
                }
            }else{
                $this->error('储值失败');
            }
        }else{
            $wx_id = I('wx_id');
            $this->assign('wx_id',$wx_id);
            $this->display();
        }

    }

    /*
     * 查看储值纪录
     * */
    public function look_record(){
        $map['uid'] = I('wx_id');
        $map['op_type'] = 99;
        $info = apiCall(UserLogApi::QUERY,array($map));
        if($info['status']){
            $this->assign('list',$info['info']['list']);
            $this->assign('show',$info['info']['show']);
        }
        $this->display();
    }

    /*
     * 增加消费奖励
     * */
    public function vip_reward($wx_id,$vip_reward,$add_money){

        $map['id'] = $wx_id;
        $lastinfo = apiCall(WxuserApi::GET_INFO,array($map));
        if($lastinfo['status']){

            $lastmoney = $lastinfo['info']['money'];

            $nowmoney = bcadd($lastmoney,$add_money*$vip_reward,3);
            $save['money'] = $nowmoney;

            $res = apiCall(WxuserApi::SAVE,array($map,$save));

        }
    }

}